package org.ehe.business.ocr.controller;


import lombok.extern.slf4j.Slf4j;
import org.ehe.business.ocr.domain.OcrLicenseResult;
import org.ehe.business.ocr.service.OcrService;
import org.ehe.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;



/**
 * OCR识别
 * @author :zhangnn
 * @className :BusinessLicenseController
 * @description: TODO
 * @date 2025-07-22 16:32:07
 */
@Slf4j
@RestController
@RequestMapping("/ocr/license")
public class OcrController {

    @Autowired
    private OcrService ocrService;

    /**
     * 营业执照识别
     * @param file
     * @return
     */
    @PostMapping("/recognize")
    public R<OcrLicenseResult> recognizeFromFile(@RequestParam("file") MultipartFile file) {
        try {
            OcrLicenseResult result = ocrService.recognizeFromFile(file);
            if (result.hasContent()) {
                log.info("营业执照识别成功，公司名称: {}", result.companyName());
                return R.ok(result);
            } else {
                log.warn("未识别到有效的营业执照信息");
                return R.fail("未识别到有效的营业执照信息");
            }

        } catch (IllegalArgumentException e) {
            log.warn("请求参数错误: {}", e.getMessage());
            return R.fail(400, e.getMessage());
        } catch (Exception e) {
            R.fail("营业执照识别失败", e);
            return R.fail("营业执照识别失败: " + e.getMessage());
        }
    }
}
